<template>
  <div class="login">
    <div class="logo_top">
      <div class="logo_top_box">
        <h2>邀请制消费返现系统</h2>
        <span>首页</span>
      </div>
    </div>
    <el-row class="login_box">
      <el-col :span="14" class="login_box_font">
        <h1>椿去湫来 海棠花开</h1>
        <p>所有活着的人类，都是海里一条巨大的鱼</p>
        <p>出生的时候他们从海的彼岸出发他们的生命就像横越大海</p>
        <p>他们便到了岸，各去各的世界</p>
      </el-col>
      <el-col :span="10" class="login_box_login">
        <h3>登录</h3>
        <el-form ref="loginForm" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="请输入账号">
              <i slot="prefix" class="el-input__icon el-icon-adm-user" style="font-size: 18px;"></i>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input :type="passwordType" v-model="form.password" placeholder="请输入密码">
              <i slot="prefix" class="el-input__icon el-icon-adm-password" style="font-size: 18px;"></i>
              <i slot="suffix" class="el-input__icon el-icon-view" style="cursor: pointer;"
                 @click="_togglePasswordType"></i>
            </el-input>
          </el-form-item>

          <el-form-item prop="yanzhengma">
            <div class="check-code-wrapper">
              <div class="yanzhengma-wrapper">
                <el-input v-model="form.yanzhengma" @keyup.enter.native="login('loginForm')" placeholder="请输入验证码">
                  <i slot="prefix" class="el-input__icon el-icon-adm-vertification" style="font-size: 18px;"></i>
                </el-input>
              </div>
              <div class="validate-code-wrapper">
                <validate-code ref="validate-code" @change="_setCheckCode"></validate-code>
              </div>
            </div>
          </el-form-item>
          <el-button type="primary" style="width:100%; background:#FF614C; border:1px solid #FF614C; margin-top:15px;" @click="loginHandle('loginForm')">登录</el-button>
          <div class="the">
            <span>忘记密码</span>
            <span>免费注册</span>
          </div>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import validateCode from "src/components/ValidateCode/index";
import axios from 'axios'
export default {
  components: {
    validateCode
  },
  data() {
    var checkYanzhengma = (rule, value, callback) => {
      value = value.toUpperCase();
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else if (value !== this.checkCode) {
        callback(new Error("验证码错误"));
        this.$refs["validate-code"].draw();
      } else {
        callback();
      }
    };
    return {
      passwordType: "password",
      checkCode: "",
      form: {
        username: "",
        password: "",
        yanzhengma: ""
      },
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        yanzhengma: [{ validator: checkYanzhengma, trigger: "blur" }]
      }
    };
  },
  methods: {
    _setCheckCode(value) {
      this.checkCode = value;
    },
    _togglePasswordType() {
      if (this.passwordType === "password") {
        this.passwordType = "text";
      } else {
        this.passwordType = "password";
      }
    },
    loginHandle(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.login();
        }
      });
    },
    login() {
      var params = new FormData();
      params.append("user_name", this.form.username);
      params.append("password", this.form.password);
      let config = {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
      }
      axios.post("api/member/api/login/", params, config).then(res => {
        console.log(res);
        let userId = '3453534543';
        this.$store.commit('SET_USERID', userId);
        this.$router.replace('home');
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.login {
  font-family: '微软雅黑';
  color: #ffffff;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-image: url("../../assets/img/login_bj.jpg");
  background-repeat: no-repeat;
  background-position: top;
  background-size: cover;
  .logo_top {
    height: 70px;
    border-bottom: 1px solid #303030;
    .logo_top_box {
      display: flex;
      justify-content: space-between;
      width: 1200px;
      margin: 0 auto;
      height: 70px;
      span {
        line-height: 70px;
        font-size: 16px;
      }
    }
  }
  .login_box {
    padding-top: 120px;
    width: 1200px;
    margin: 0 auto;
    .login_box_font {
      padding-top: 50px;
      h1 {
        font-size: 40px;
        font-family: PingFangSC-Semibold;
        letter-spacing: 0;
      }
      p {
        line-height: 32px;
      }
    }
    .login_box_login {
      padding: 15px;
      color: #303030;
      width: 400px;
      height: 380px;
      border-radius: 10px;
      background: #ffffff;
      h3 {
        line-height: 0;
        padding-bottom: 20px;
      }
    }
  }
}

.check-code-wrapper {
  display: flex;
  justify-content: space-between;
  height: 40px;

  .yanzhengma-wrapper {
    flex: 0 1 auto;
  }

  .validate-code-wrapper {
    flex: 0 0 160px;
  }
}
.the {
  display: flex;
  justify-content: space-between;
  padding: 5px 0;
  font-size: 14px;
  color: #4a4a4a;
}
</style>
